package com.ridaizu.dao.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.ridaizu.dao.DaoAdapter;
import com.ridaizu.dao.db.DBAccess;
import com.ridaizu.model.GoodsManager;
import com.ridaizu.model.Model;
import com.ridaizu.model.search.SearchModel;

@ManagedBean(name = "goodsManagerDao")
@ApplicationScoped
public class GoodsManagerDao extends DaoAdapter {

	private static final long serialVersionUID = 1L;

	public int selectCount(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from m_goods_manager \n");
		sql.append(searchModel.getSelectCountSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		if (rs == null) {
			return -1;
		}

		rs.next();

		return rs.getInt(1);
	}

	public List<GoodsManager> selectGoodsManagerList(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT \n");
		sql.append("gm.id as id, \n");
		sql.append("gm.brand_id as brand_id, \n");
		sql.append("b.name as brand_name, \n");
		sql.append("gm.brand_series_id as brand_series_id, \n");
		sql.append("bs.name as brand_series_name, \n");
		sql.append("gm.goods_id as goods_id, \n");
		sql.append("g.name as goods_name, \n");
		sql.append("gm.rgt_opt as rgt_opt, \n");
		sql.append("gm.rgt_date as rgt_date, \n");
		sql.append("gm.upd_opt as upd_opt, \n");
		sql.append("gm.upd_date as upd_date \n");
		sql.append("FROM m_goods_manager gm \n");
		sql.append("LEFT JOIN m_brand as b on b.id = gm.brand_id \n");
		sql.append("LEFT JOIN m_brand_series as bs on bs.id = gm.brand_series_id \n");
		sql.append("LEFT JOIN m_goods as g on g.id = gm.goods_id \n");

		sql.append(searchModel.getSelectSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		List<GoodsManager> modelList = new ArrayList<GoodsManager> ();
		while (rs.next()) {
			modelList.add((GoodsManager)getModel(rs));
		}

		rs.close();

		return modelList;
	}

	@Override
	public Model getModel(ResultSet rs) throws SQLException {

		checkNotNull(rs);
		GoodsManager goodsManager = new GoodsManager();

		goodsManager.setId(rs.getInt("id"));
		goodsManager.setBrandId(rs.getInt("brand_id"));
		goodsManager.setBrandName(rs.getString("brand_name"));
		goodsManager.setBrandSeriesId(rs.getInt("brand_series_id"));
		goodsManager.setBrandSeriesName(rs.getString("brand_series_name"));
		goodsManager.setGoodsId(rs.getInt("goods_id"));
		goodsManager.setGoodsName(rs.getString("goods_name"));
		goodsManager.setRgtOpt(rs.getString("rgt_opt"));
		goodsManager.setRgtDate(rs.getTimestamp("rgt_date"));
		goodsManager.setUpdOpt(rs.getString("upd_opt"));
		goodsManager.setUpdDate(rs.getTimestamp("upd_date"));

		return goodsManager;
	}

	@Override
	public int insert(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO m_goods_manager \n");
		sql.append("( \n");
		sql.append("brand_id, brand_series_id, goods_id, rgt_opt, \n");
		sql.append("rgt_date, upd_opt, upd_date \n");
		sql.append(") \n");
		sql.append("VALUES \n");
		sql.append("(?, ?, ?, ?, ?, ?, ?); \n");

		GoodsManager goodsManager = (GoodsManager) model;

		Object[] params = new Object[] {
				goodsManager.getBrandId(), goodsManager.getBrandSeriesId(), goodsManager.getGoodsId(), goodsManager.getRgtOpt(),
				goodsManager.getRgtDate(), goodsManager.getUpdOpt(), goodsManager.getUpdDate()
		};

		return DBAccess.update(sql.toString(), params);
	}

	@Override
	public int update(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE m_goods_manager \n");
		sql.append("SET \n");
		sql.append("brand_id = ?, \n");
		sql.append("brand_series_id = ?, \n");
		sql.append("goods_id = ?, \n");
		sql.append("rgt_opt = ?, \n");
		sql.append("rgt_date = ?, \n");
		sql.append("upd_opt = ?, \n");
		sql.append("upd_date = ? \n");
		sql.append("WHERE id = ? \n");

		GoodsManager goodsManager = (GoodsManager) model;

		Object[] params = new Object[] {
				goodsManager.getBrandId(), goodsManager.getBrandSeriesId(), goodsManager.getGoodsId(), goodsManager.getRgtOpt(),
				goodsManager.getRgtDate(), goodsManager.getUpdOpt(), goodsManager.getUpdDate(),
				goodsManager.getId()
		};

		return DBAccess.update(sql.toString(), params);
	}

	public int delete(int id) {
		String sql = "DELETE FROM m_goods_manager WHERE id=?;";

		return DBAccess.update(sql.toString(), id);
	}

}
